<?php
    include_once('connect.php');


    $cloths_id = $_GET['cloths_id'];
    $username = $_GET['login_user'];
    $num = $_GET['num'];
    $size = $_GET['size'];

    $res = [];
    //判断购物车中是否有这个商品
    $sql1 = "select * from cart where cloths_id = '$cloths_id' and user_name = '$username' and size = '$size'";
    $data1 = mysqli_query($conn, $sql1);
    $arr = mysqli_fetch_array($data1);

    
    if($arr){
        $sql = "update cart set num = num + $num where cloths_id = '$cloths_id' and user_name = '$username'";
        $data = mysqli_query($conn, $sql);
        
        if($data){
            $res['status'] = true;
            $res['msg'] = '商品添加成功';
        }else{
            $res['status'] = false;
            $res['msg'] = '商品添加失败';
        }
    }else{
       $sql2 = "insert into cart (user_name, cloths_id, num, size) value('$username', '$cloths_id', '$num', '$size')";
       $data2 = mysqli_query($conn, $sql2);
       if($data2){
            $res['status'] = true;
            $res['msg'] = '商品添加成功';
        }else{
            $res['status'] = false;
            $res['msg'] = '商品添加失败';
        }
    }

    echo(json_encode($res));

?>